﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.Sql;
using System.Data.SqlClient;
using BookStore.Solution.Configurations;
using BookStore.Solution.Utilities;

namespace BookStore.Solution.Presentation
{
    public partial class frmChooseServer : DevComponents.DotNetBar.OfficeForm
    {
        public string ConnectionString { get; set; }
        public frmChooseServer()
        {
            InitializeComponent();
            List<string> name = GetNetworkInstances();
            foreach (string n in name)
                cbServerName.Items.Add(n);
            if (name.Count > 0)
            {
                cbServerName.SelectedIndex = 0;
            }
            else
            {
                MessageBox.Show("Không tìm thấy server nào", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Close();
            }
        }
        public static List<string> GetNetworkInstances()
        {
            var rel = new List<string>();
            var dt = SqlDataSourceEnumerator.Instance.GetDataSources();
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var row = dt.Rows[i];
                var name = row["ServerName"].ToString();
                var ins = DataUtil.ToString(row["InstanceName"],"") =="" ? "" : "\\" + row["InstanceName"];
                
                rel.Add(name + ins);
            }
            return rel;
        }

        bool CheckValidated()
        {
            txtPassword.Border.BackColor = txtUsername.Border.BackColor = StyleManager.ColorTint;
            bool t = true;
            if (txtUsername.Text == "")
            {
                txtUsername.Border.BackColor = Color.Red;
                t = false;
            }
            if (txtPassword.Text == "")
            {
                txtPassword.Border.BackColor = Color.Red;
                t = false;
            }
            return t;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            ConnectionString = "";
            if (CheckValidated())
            {
                try
                {
                    ConnectionString = "server={0}; Database=BookStore;user id={1};password={2};";
                    SqlConnection conn = new SqlConnection(String.Format(ConnectionString, cbServerName.Text, txtUsername.Text, txtPassword.Text));
                    conn.Open();
                    MessageBox.Show("Kết nối thành công, Bạn cần khởi động lại chương trình!!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    conn.Close();
                    //Server
                    Configs.EditConnectionString("Server", cbServerName.Text);
                    Configs.EditConnectionString("UserID", txtUsername.Text);
                    Configs.EditConnectionString("Password", txtPassword.Text);
                    Configs.EditConnectionString("BookStore.Solution.Presentation.Properties.Settings.BookStoreConnectionString",
                        "Data Source="+ cbServerName.Text +";Initial Catalog=BookStore;Persist Security Info=True;User ID="+ txtUsername.Text +";Password=" + txtPassword.Text,
                        "System.Data.SqlClient");
                    Configs.EditConnectionString("BookStore.Solution.Presentation.Properties.Settings.BookStoreConnectionString1",
                        "Data Source=" + cbServerName.Text + ";Initial Catalog=BookStore;Persist Security Info=True;User ID=" + txtUsername.Text + ";Password=" + txtPassword.Text,
                        "System.Data.SqlClient");
                    if (Configs.FirstLaunch == "True")
                        Configs.EditAppSetting("FirstLaunch", "False");
                    Application.Restart();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ConnectionString = null;
            Close();
        }
    }
}